package cn.org.eshow.service.impl;

import cn.org.eshow.bean.query.MoneyQuery;
import cn.org.eshow.common.ShopDataVar;
import cn.org.eshow.common.page.Page;
import cn.org.eshow.dao.MoneyDao;
import cn.org.eshow.model.Bill;
import cn.org.eshow.model.Money;
import cn.org.eshow.model.User;
import cn.org.eshow.model.Withdraw;
import cn.org.eshow.service.BillManager;
import cn.org.eshow.service.MoneyManager;
import cn.org.eshow.util.OrderUtil;
import cn.org.eshow.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.jws.WebService;
import java.util.Date;
import java.util.List;

@WebService(serviceName = "MoneyService", endpointInterface = "cn.org.eshow.service.MoneyManager")
@Service
public class MoneyManagerImpl extends GenericManagerImpl<Money, Integer> implements MoneyManager {

    @Autowired
    MoneyDao moneyDao;
    @Autowired
    BillManager billManager;

    public MoneyManagerImpl() {
    }

    @Autowired
    public MoneyManagerImpl(MoneyDao moneyDao) {
        super(moneyDao);
        this.moneyDao = moneyDao;
    }

    @Override
    public List<Money> list(MoneyQuery query) {
        return moneyDao.list(query);
    }

    @Override
    public Page<Money> search(MoneyQuery query) {
        return moneyDao.search(query);
    }

    @Override
    public Money mine(User user) {
        List<Money> moneys = moneyDao.list(new MoneyQuery(user.getId()));
        if (moneys.isEmpty()) {
            Money money = new Money();
            money.setUser(user);
            return moneyDao.save(money);
        }
        return moneys.get(0);
    }

    @Override
    public Money check(MoneyQuery query) {
        List<Money> list = moneyDao.list(query);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public Money balance(Money money, Double amount) {
        money.setUpdateTime(new Date());
        money.setBalance(StringUtil.toDecimal((money.getBalance() + amount), 2));
        money = moneyDao.save(money);
        return money;
    }


    @Override
    public Money update(Money old, Money money) {
        return moneyDao.save(old);
    }

    @Override
    public Money frozen(Money money, Double amount) {
        money.setUpdateTime(new Date());
        money.setFrozen(StringUtil.toDecimal((money.getFrozen() + amount), 2));
        money = moneyDao.save(money);
        return money;
    }

    @Override
    public Money withdrawing(Money money, Double amount) {
        money.setUpdateTime(new Date());
        money.setWithdrawing(StringUtil.toDecimal((money.getWithdrawing() + amount), 2));
        money = moneyDao.save(money);
        return money;
    }

    @Override
    public Money withdraw(Money money, Double amount) {
        money.setUpdateTime(new Date());
        money.setWithdraw(StringUtil.toDecimal((money.getWithdraw() + amount), 2));
        money = moneyDao.save(money);
        return money;
    }

    @Override
    public Boolean withdraw(Money money, Withdraw withdraw) {
        if (money.getBalance() < withdraw.getPrice()) {
            return false;
        }
        money = this.balance(money, -withdraw.getPrice());
        money = this.withdrawing(money, withdraw.getPrice());

        Bill bill = new Bill();
        bill.setAddTime(new Date());
        bill.setNo(OrderUtil.getOrderNo());
        bill.setCategory(ShopDataVar.billCategory.用户.toString());
        bill.setType(ShopDataVar.billType.提现.toString());
        bill.setReceive(0.0);
        bill.setOutlay(withdraw.getPrice());
        bill.setBalance(money.getBalance());
        bill.setOrderType("withdraw");
        bill.setOrderNo(withdraw.getNo());
        bill.setState(1);
        bill.setUser(withdraw.getUser());
        billManager.save(bill);
        return true;
    }




}